﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Utility;

namespace CookingPlan
{
    class PlanFile
    {
        private static readonly XmlDocument Plan = new XmlDocument();

        static PlanFile()
        {
            Plan.LoadXml("<?xml version=\"1.0\"?><plan></plan>");
        }

        /// <summary>
        /// 获取烹饪计划文件
        /// </summary>
        /// <param name="recipes">菜谱文件的集合</param>
        /// <param name="ingredients">食材报告文件中的食材有序集合</param>
        /// <param name="tuples">菜谱和时间的对应关系</param>
        /// <param name="path">烹饪计划文件的保存路径</param>
        public static void GetPlanFile(IEnumerable<XmlDocument> recipes, List<Ingredient> ingredients,
            IEnumerable<TimeRecipeTuple> tuples, string path)
        {
            AddTuples(tuples);
            AddRecipes(recipes, ingredients);

            Plan.Save(path + "plan.plan");
        }

        //添加对应关系
        private static void AddTuples(IEnumerable<TimeRecipeTuple> ts)
        {
            XmlElement tuples = Plan.CreateElement("tuples");

            foreach(var t in ts)
            {
                XmlElement tuple = Plan.CreateElement("tuple");
                tuple.SetAttribute("time", t.Time);
                tuple.SetAttribute("recipeid", t.RecipeFile);
                tuples.AppendChild(tuple);
            }

            XmlNode plan = Plan.GetElementsByTagName("plan")[0];
            plan.AppendChild(tuples);
        }

        //添加菜谱
        private static void AddRecipes(IEnumerable<XmlDocument> rs, List<Ingredient> ings)
        {
            XmlElement recipes = Plan.CreateElement("recipes");

            int i = 0;
            foreach(var r in rs)
            {
                XmlElement recipe = Plan.CreateElement("recipe");
                recipe.SetAttribute("id", i.ToString());
                recipe.SetAttribute("version", "1.0.2012.3");

                //自定义函数
                XmlNode fs = r.GetElementsByTagName("functions")[0];
                XmlNode functions = Plan.ImportNode(fs, true);
                recipe.AppendChild(functions);

                //主体部分
                XmlNode cing = r.GetElementsByTagName("cooking")[0];
                XmlNode cooking = Plan.ImportNode(cing, true);
                XmlNodeList cmds = cooking.ChildNodes;
                //获取食材说明部分，供后边使用
                XmlNodeList ingredients = r.GetElementsByTagName("ingredients")[0].ChildNodes;
                //需要修改INPUT命令，由食材名称改为ID，并且注明称量和度量
                foreach (XmlElement cmd in cmds)
                {
                    if(cmd.GetAttribute("name") == "INPUT")
                    {
                        //提供食材名字的参数
                        var argName = (XmlElement)cmd.ChildNodes[0];
                        //添加称量和度量
                        XmlElement amount = Plan.CreateElement("argument");
                        XmlElement dimension = Plan.CreateElement("argument");
                        //在食材说明部分中寻找数据
                        foreach(XmlElement ing in ingredients)
                        {
                            if(ing.GetAttribute("name") == argName.GetAttribute("value"))
                            {
                                amount.SetAttribute("value", ing.GetAttribute("amount"));
                                dimension.SetAttribute("value", ing.GetAttribute("dimension"));
                            }
                        }
                        cmd.AppendChild(amount);
                        cmd.AppendChild(dimension);
                        //修改名称
                        argName.SetAttribute("value",
                                             ings.FindIndex(ing => ing.Name == argName.GetAttribute("value")).ToString());
                    }
                }
                recipe.AppendChild(cooking);

                recipes.AppendChild(recipe);
                i++;
            }

            XmlNode plan = Plan.GetElementsByTagName("plan")[0];
            plan.AppendChild(recipes);
        }
    }
}